package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.knowledge.domain.dto.FilesDistributeSenderDto;
import cn.cdeden.knowledge.domain.dto.FilesDistributeSenderQueryParam;
import cn.cdeden.knowledge.utils.PageUtil;
import cn.cdeden.system.api.RemoteUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cdeden.knowledge.mapper.FilesDistributeSenderMapper;
import cn.cdeden.knowledge.domain.FilesDistributeSender;
import cn.cdeden.knowledge.service.FilesDistributeSenderService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = FilesDistributeSenderService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FilesDistributeSenderServiceImpl extends ServiceImpl<FilesDistributeSenderMapper, FilesDistributeSender> implements FilesDistributeSenderService{

    private final FilesDistributeSenderMapper filesDistributeSenderMapper;

    @DubboReference(check = false)
    private final RemoteUserService remoteUserService;

    @Override
    public List<FilesDistributeSenderDto> getByUserId(Long userId) {
        FilesDistributeSenderQueryParam query = new FilesDistributeSenderQueryParam();
        QueryWrapper<FilesDistributeSender> predicate = QueryHelpMybatisPlus.getPredicate(query);
        predicate.eq("sender_id", userId);
        List<FilesDistributeSender> filesDistributeSenders = filesDistributeSenderMapper.selectList(predicate);
        return MapstructUtils.convert(filesDistributeSenderMapper.selectList(predicate), FilesDistributeSenderDto.class);
//        return ConvertUtil.convertList(filesDistributeSenderMapper.selectList(predicate), FilesDistributeSenderDto.class);
    }

    @Override
    public TableDataInfo<FilesDistributeSenderDto> queryAll(FilesDistributeSenderQueryParam query, Pageable pageable) {
        IPage<FilesDistributeSender> queryPage = PageUtil.toMybatisPage(pageable);
        QueryWrapper<FilesDistributeSender> predicate = QueryHelpMybatisPlus.getPredicate(query);
        //未接收的优先显示
        IPage<FilesDistributeSender> page = filesDistributeSenderMapper.selectPage(queryPage, predicate);
        TableDataInfo<FilesDistributeSender> build = TableDataInfo.build(page);

        TableDataInfo<FilesDistributeSenderDto> filesDistributeSenderDtoTableDataInfo = MapstructUtils.convertTableDataInfo(build, FilesDistributeSenderDto.class);
        List<FilesDistributeSenderDto> content = filesDistributeSenderDtoTableDataInfo.getRows();
        for (FilesDistributeSenderDto filesDistributeSender : content) {
            filesDistributeSender.setSenderName(remoteUserService.selectUserByUserId(filesDistributeSender.getSenderId().longValue()).getNickName());
        }
        filesDistributeSenderDtoTableDataInfo.setRows(content);
        return filesDistributeSenderDtoTableDataInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(FilesDistributeSenderDto resources) {
        FilesDistributeSender entity = MapstructUtils.convert(resources, FilesDistributeSender.class);
        return filesDistributeSenderMapper.insert(entity);
    }

    @Override
    public boolean updateReceive(Long userId, Integer distributeId) {
        UpdateWrapper<FilesDistributeSender> updateWrapper = new UpdateWrapper<FilesDistributeSender>()
            .eq("distribute_id",distributeId)
            .set("receive",1)
            .eq("sender_id",userId);
        return update(updateWrapper);
    }
}

